using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WbsEditor
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string []args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (args.Length == 0)
            {
                StartAppWithNewWbs();
            }
            else
            {
                CheckArguments(args);
            }
        }

        private static void StartAppWithNewWbs()
        {
            Application.Run(new MainForm());
        }

        private static void CheckArguments(string[] args)
        {
            string arg = args[0];
            string protocolPrefix = "wbsedit://";
            if (arg.StartsWith(protocolPrefix))
            {
                string location = arg.Substring(protocolPrefix.Length);
                if (location.Length > 0)
                {
                    if (char.IsNumber(location[0]))
                    {
                        // Strip trailing slash
                        if (location.EndsWith("/"))
                        {
                            location = location.Substring(0, location.Length - 1);
                        }
                        int workItemId = -1;
                        if (int.TryParse(location, out workItemId))
                        {
                            StartAppWithWorkItem(workItemId);
                            return;
                        }
                    }
                    else
                    {
                        StartAppWithXmlFile(location);
                        return;
                    }
                }
            }
            else if (arg.StartsWith("-"))
            {
                // Check options and stuff
            }
            else
            {
                StartAppWithXmlFile(arg);
                return;
            }

            MessageBox.Show("Unknown Argument:\n\n" + arg, "WBS Editor", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            StartAppWithNewWbs();
        }

        private static void StartAppWithXmlFile(string location)
        {
            MainForm form = new MainForm();
            form.StartupXmlFile = location;
            Application.Run(form);
        }

        private static void StartAppWithWorkItem(int workItemId)
        {
            MainForm form = new MainForm();
            form.StartupWorkItem = workItemId;
            Application.Run(form);
        }
    }
}